import { Texture } from './Texture';

export interface FramebufferCreateInfo {
  width: number;
  height: number;
  colorTexture?: WebGLTexture | null;
  createDepthBuffer?: boolean;
}

export class Framebuffer {
  private gl: WebGLRenderingContext;
  private framebuffer: WebGLFramebuffer | null = null;
  private colorTexture: WebGLTexture | null = null;
  private depthRenderbuffer: WebGLRenderbuffer | null = null;
  private width: number = 0;
  private height: number = 0;

  constructor(gl: WebGLRenderingContext) {
    this.gl = gl;
  }

  create(info: FramebufferCreateInfo): boolean {
    const gl = this.gl;
    
    // 保存尺寸
    this.width = info.width;
    this.height = info.height;
    
    // 创建帧缓冲
    const framebuffer = gl.createFramebuffer();
    if (!framebuffer) {
      console.error('创建帧缓冲失败');
      return false;
    }
    
    // 绑定帧缓冲
    gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
    
    // 使用提供的颜色纹理或创建新的
    let colorTexture = info.colorTexture;
    if (!colorTexture) {
      // 创建颜色纹理
      const texture = new Texture(gl);
      if (!texture.createEmptyTexture(info.width, info.height)) {
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
        return false;
      }
      colorTexture = texture.getTexture();
      if (!colorTexture) {
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
        return false;
      }
    }
    
    // 附加颜色纹理到帧缓冲
    gl.framebufferTexture2D(
      gl.FRAMEBUFFER, 
      gl.COLOR_ATTACHMENT0, 
      gl.TEXTURE_2D, 
      colorTexture, 
      0
    );
    this.colorTexture = colorTexture;
    
    // 如果需要，创建深度缓冲
    if (info.createDepthBuffer) {
      const depthRenderbuffer = gl.createRenderbuffer();
      if (!depthRenderbuffer) {
        console.error('创建深度渲染缓冲失败');
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
        return false;
      }
      
      gl.bindRenderbuffer(gl.RENDERBUFFER, depthRenderbuffer);
      gl.renderbufferStorage(
        gl.RENDERBUFFER, 
        gl.DEPTH_COMPONENT16, 
        info.width, 
        info.height
      );
      
      gl.framebufferRenderbuffer(
        gl.FRAMEBUFFER, 
        gl.DEPTH_ATTACHMENT, 
        gl.RENDERBUFFER, 
        depthRenderbuffer
      );
      
      this.depthRenderbuffer = depthRenderbuffer;
    }
    
    // 检查帧缓冲是否完整
    const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
    if (status !== gl.FRAMEBUFFER_COMPLETE) {
      console.error('帧缓冲不完整:', status);
      gl.bindFramebuffer(gl.FRAMEBUFFER, null);
      return false;
    }
    
    // 解绑帧缓冲
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    
    // 保存帧缓冲
    this.framebuffer = framebuffer;
    
    return true;
  }

  bind(): void {
    if (this.framebuffer) {
      const gl = this.gl;
      gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
      gl.viewport(0, 0, this.width, this.height);
    }
  }

  unbind(): void {
    const gl = this.gl;
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  }

  getColorTexture(): WebGLTexture | null {
    return this.colorTexture;
  }

  getWidth(): number {
    return this.width;
  }
  
  getHeight(): number {
    return this.height;
  }

  cleanup(): void {
    const gl = this.gl;
    
    if (this.depthRenderbuffer) {
      gl.deleteRenderbuffer(this.depthRenderbuffer);
      this.depthRenderbuffer = null;
    }
    
    if (this.framebuffer) {
      gl.deleteFramebuffer(this.framebuffer);
      this.framebuffer = null;
    }
    
    // 注意：我们不删除颜色纹理，因为它可能被外部使用
  }
} 